﻿/****************************** Module Header ******************************\
 * Module Name:  SwitchControl.cs
 * Module Version:  1.0.0 (2012-10-19)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This componse is use for SwitchControl.
 * #Note: 狗日的,啃了一个早上才搞定...
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace UCL.Web.CustomControls
{
    /// <summary>
    /// 开关控件
    /// </summary>
    /// <example>
    /// <![CDATA[
    /// <so:SwitchControl runat="server" ID="loginPanel" TagName="span" SiteTag="xinyou" LoadCompleteFunc="afterDo"><OnTemplate>尊敬的:{uname}{dmslink}<a href="#">[退出]</a><a href="#">会员中心</a></OnTemplate><OffTemplate>请<a href="#">登录</a>或<a href="#">免费注册</a></OffTemplate></so:SwitchControl>
    /// <script>function afterDo() {alert($(this).html()); };</script>
    /// ]]>
    /// </example>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:SwitchControl runat=server></{0}:SwitchControl>")]
    [ParseChildren(true)]
    public class SwitchControl : Control, INamingContainer
    {
        /// <summary>
        /// 定时器，单位是毫秒
        /// </summary>
        public int Timmer { get; set; }

        /// <summary>
        /// 站点标识
        /// </summary>
        public string SiteTag { get; set; }

        /// <summary>
        /// 自定义标签名称
        /// </summary>
        public string TagName { get; set; }

        /// <summary>
        /// 数据异步加载完成后执行的Js函数
        /// </summary>
        public string LoadCompleteFunc { get; set; }

        [TemplateContainer(typeof(TemplateContainer))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual ITemplate OffTemplate { get; set; }

        [TemplateContainer(typeof(TemplateContainer))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual ITemplate OnTemplate { get; set; }

        TemplateContainer tcon, tcoff;

        protected override void OnPreRender(EventArgs e)
        {
            Page.PreRenderComplete += PagePreRenderComplete;
            base.OnPreRender(e);
        }
        protected void PagePreRenderComplete(object s, EventArgs e)
        {
            EnsureChildControls();
            if (Timmer == 0) Timmer = 3000;
            LoadCompleteFunc = string.IsNullOrEmpty(LoadCompleteFunc) ? "" : ",{0}".Fmt(LoadCompleteFunc);
            Page.ClientScript.RegisterStartupScript(typeof(Page), this.UniqueID, "var getLoginInfo{0}=function(){{$('#{0}').load('/DMS/Apis/o/apiModules.ashx?rnd=' + Math.random(), {{on:'{1}',off:'{2}',sitetag:'{3}',event:'getinfo',module:'Login'}}{4});}};getLoginInfo{0}();setInterval(getLoginInfo,{5});".Fmt(this.UniqueID, tcon.InnerHtml, tcoff.InnerHtml, SiteTag, LoadCompleteFunc,Timmer), true);
        }
        protected override void CreateChildControls()
        {
            if (OnTemplate != null)
            {
                tcon = new TemplateContainer();
                OnTemplate.InstantiateIn(tcon);
            }
            if (OffTemplate != null)
            {
                tcoff = new TemplateContainer();
                OffTemplate.InstantiateIn(tcoff);
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (string.IsNullOrEmpty(TagName)) TagName = "div";
            writer.Write(string.Format("<{0} id=\"{1}\"></{0}>", this.TagName, this.UniqueID));
        }

        /// <summary>
        /// 模板控件类型
        /// </summary>
        public class TemplateContainer : HtmlContainerControl, INamingContainer
        {
            protected override void Render(HtmlTextWriter writer)
            {
                writer.Write(this.InnerHtml);
            }
        }
    }
}
